package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"yzy6.20work/global"
	"yzy6.20work/internal/model"
)

func MysqlLink() {
	DB := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", global.App.Mysql.User, global.App.Mysql.Password, global.App.Mysql.Host, global.App.Mysql.Port, global.App.Mysql.Database)
	var err error
	global.DB, err = gorm.Open(mysql.Open(DB))
	if err != nil {
		panic("failed to connect database")
	}
	if err = global.DB.AutoMigrate(&model.User{}, &model.Good{}, &model.Video{}); err != nil {
		panic(err)
	}
	fmt.Println("MysqlLink auto migrate success")
}
